require('./functions.js')
var fs = require("fs")

hostname_isrc = 'www.api.apprevolve.com'
path_isrc = '/v2/getAds?siteid=48805&token=Ley6LisEiE2hX7IPZ2jcPA'

updateApi_isrc = function(str)
{      
    try { var obj = eval('(' + str + ')')}
    catch (e) {console.log('error');return}

    var mysql  = require('mysql');
    // var connection = mysql.createConnection({host: 'rdsea0aj9n7m3g48ta5u.mysql.rds.aliyuncs.com',
    //  database: 'ad688com', 
    //  user:'ad688com',
    //  password : 'ad688com',
    //  port: '3306',
    //  dateStrings: true ,
    //  multipleStatements: true}); 
    var connection = mysql.createConnection({host: 'game1715.com',
    database: 'test', 
    user:'t',
    password : 's123',
    port: '3306',
    dateStrings: true ,
    multipleStatements: true}); 

    connection.connect() 

    var ads = obj.ads
    var createSql = 'CREATE TABLE IF NOT EXISTS `api_isrc_campaigns` (`id` int(11) primary key NOT NULL auto_increment,`updateTime` text,`version_code` varchar(255) DEFAULT NULL,`offer_id` int(11) NOT NULL,`internal_offer_id` int(11) NOT NULL,`campaign_id` int(11) NOT NULL,`internal_campaign_id` int(11) NOT NULL,`bid` varchar(255) DEFAULT NULL,`platform` varchar(255) DEFAULT NULL,`minOSVersion` varchar(255) NOT NULL,`creatives` text,`pricingModel` varchar(255) DEFAULT NULL,`geoTargeting` text,`packageSize` text,`packageSizeInBytes` varchar(255) NOT NULL,`downloads` varchar(255) DEFAULT NULL,`rating` varchar(255) DEFAULT NULL,`category` text,`deviceTypes` text,`connectionType` text,`score` varchar(255) DEFAULT NULL,`targetSdkVersion` text,`packageName` text,`title` text,`description` text,`clickURL` text,`impressionURL` text,`global` varchar(255) NOT NULL,`offMarket` varchar(255) NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT;'

    connection.query(createSql+"TRUNCATE TABLE `api_isrc_campaigns`;", function(err, rows, fields) 
    { 
         if (err) {
                 console.log('[query] - :'+err);
            return;
         }
         for (var i=0;i<ads.length;i++)
            {   
                console.log('INSERT params1:',i)  
                var ad = ads[i]
                ad.updateTime = currentTime()
                var element =['id','updateTime','version_code', 'offer_id', 'internal_offer_id', 'campaign_id', 'internal_campaign_id', 'bid', 'platform', 'minOSVersion', 'creatives', 'pricingModel', 'geoTargeting', 'packageSize', 'packageSizeInBytes', 'downloads', 'rating', 'category', 'deviceTypes', 'connectionType', 'score', 'targetSdkVersion', 'packageName', 'title', 'description', 'clickURL', 'impressionURL', 'global', 'offMarket']
                var sql1 = '('
                for (var j=0;j<element.length;j++)
                {
                    sql1 = sql1 + element[j] 
                    if (j == element.length -1)
                        sql1 = sql1 + ')'
                    else
                        sql1 = sql1 + ','
                }
                var  sql = 'INSERT INTO api_isrc_campaigns' + sql1 + 'VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)';
                var  params = [];
                for (var j=0;j<element.length;j++)
                {   
                    if (j==0)
                        params[j] = null
                    else
                        params[j] = xToString(ad[element[j]] )     
                          
                }
            console.log('INSERT params:',params[1],ad.offer_id)  
                connection.query(sql,params, 
                    function(err, rows) 
                    { 
                        if (err) 
                        {
                            console.log('INSERT error:',err.message);
                            return
                        }
                        console.log('INSERT success:',rows);    
                    })

            }
    });  
}





